import Vue from 'vue';
import db from '@/utils/storage';
import { permissionConfig } from '@/config/permisionConfig';
import store from '@/store'
// 处理函数
const handler = (el, config) => {
  const { value, modifiers } = config;
  const button = store.state.menu.resources
  let isRemove = false;
  if (Array.isArray(value) && !modifiers.and) {
    isRemove = value.filter(key => button.some(v => v.uri === permissionConfig[key])).length <= 0;
  }
  else if (Array.isArray(value) && modifiers.and) {
    isRemove = value.filter(item => button.some(v => v.nameItemLabel === item)).length !== value.length;
  } else {
    isRemove = !button.some(v => v.nameItemLabel === value);
  }
  if (isRemove) {
    el.remove();
  }
};

/**
 * 权限指令配合route使用
 * eg:
 * v-permission="resourceId"
 * v-permission="['resourceId1', 'resourceId2']"
 */
Vue.directive('permission', {
  inserted: handler,
  update: handler,
  componentUpdated: handler
});
